<?php
class ListUserAdminForm extends Form
{
	function ListUserAdminForm()
	{
		Form::Form("ListUserAdminForm");
		
		CGlobal::$website_title="Quản trị thành viên";	
		
		$this->link_css('style/manage_content.css');
		$this->link_css('style/ui.datepicker.css');
		$this->link_css('style/style_cart.css');
		
		$this->link_js('javascript/jquery/ui.datepicker.js');	
		$this->link_js('javascript/payment/cartGold.js');		
		$this->link_js('javascript/library.js');	
		$this->link_js('javascript/admin.js');
	}
	
	function on_submit()
	{
		$arr = isset($_POST["chk_id"])?$_POST["chk_id"]:array();
		
		$str_ids = "";
		if(count($arr)>0)
		{
			for($i=0;$i<count($arr);$i++){
				$str_ids .=($str_ids?",":"").$arr[$i];
			}
			if($str_ids){
				$this->lock_forever($str_ids);
			}			
		}
	}	
	
	function draw()
	{
		global $display;
		$this->beginForm(false,"POST",false,Url::build_current());	
		
		$items = array();	
		$created_time_from=0;
		$created_time_to=0;
		$condition = "";
		$str_ids = "";
		$paging = "";
		$limit="";
		$lock_log = array();
		$arrUser_id = array();
		
		$orderby  = "id DESC ";
		
		$arr_field = array("id","email","full_name","mobile_phone","reg_ip","last_ip","create_time","last_login","block_time");
		
		// Search Active
		$active = Url::get("active","all");
		$display->add("active",$active);
		
		if($active == "active"){	
			$condition .= ($condition?" AND ":"")."is_active = 0";
		}
		elseif($active == "unactive"){
			$condition .= ($condition?" AND ":"")."is_active = 1";
		}
		
		// Search lock
		$block = Url::get("block","all");
		$display->add("block",$block);
		
		if($block == "yes"){	
			$condition .= ($condition?" AND ":"")."( block_time >=".TIME_NOW." OR block_time=-1 )";
		}
		elseif($block == "no"){
			$condition .= ($condition?" AND ":"")."( block_time < ".TIME_NOW." AND block_time>-1 )";
		}
		
		$type_search = Url::get("type_search","email");
		$display->add("type_search",$type_search);
		
		$date_type_search = Url::get("date_type_search","create_time");
		$display->add("date_type_search",$date_type_search);
		
		$search = String::trimSpace(Url::get("search",""));
		
		$display->add("search",$search);
		
		if($search && in_array($type_search,$arr_field)){
			if($type_search == "user_id"){	
				$condition .= ($condition?" AND ":"")."$type_search = ".(int)$search;
			}
			elseif($type_search == "email" || $type_search == "full_name" || $type_search == "mobile_phone"){
				$condition .= ($condition?" AND ":"")."$type_search = '$search'";
			}
			elseif($type_search == "all"){
				$condition .= ($condition?" AND ":"")." (email = '$search' || full_name = '$search') ";
			}
			else{
				$condition .= ($condition?" AND ":"")."$type_search = '$search'";
			}
		}
		
		$created_time_from = Url::get("created_time_from");
		$created_time_to = Url::get("created_time_to");
		
		$display->add("created_time_from",$created_time_from);
		$display->add("created_time_to",$created_time_to);	
			
		if($created_time_from){
			$date_arr = explode('-',$created_time_from);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		if($created_time_to){
			$date_arr = explode('-',$created_time_to);
			
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}

		if($created_time_from && in_array($date_type_search,$arr_field)){																	
			$condition .= ($condition?" AND ":"")."$date_type_search >= $created_time_from";
		}
		
		if($created_time_to && in_array($date_type_search,$arr_field))
		{																	
			$condition .= ($condition?" AND ":"")."$date_type_search <= $created_time_to";
		}
		
		$condition = ($condition?" WHERE ":"").$condition;
						
		$item_per_page 	= 	Url::get("item_per_page",50);
		$sql_count		=	"SELECT COUNT(*) AS total_item FROM user ".$condition;
		$total			=	DB::fetch($sql_count,"total_item",0);
		
		if($total)
		{
			$paging = ECPaging::pagingSE($limit,$total,$item_per_page,10,"page_no",true,"Thành viên","Trang");			
			
			$sql = "SELECT * FROM user ".$condition." ORDER BY $orderby ".$limit;

			$result = DB::query($sql);
			
			if($result)
			{				
				while ($row=mysql_fetch_assoc($result))
				{
					$arrUser_id[$row["id"]] = $row["id"];
					
					$row["create_time"] = ($row["create_time"]?date("d/m/y H:i",$row["create_time"]):"");
					
					if($row["last_login"]){
						$row["last_login"] = $row["last_login"]?date("d/m/y H:i",$row["last_login"]):"";
					}
					$row["is_block"] = ($row["block_time"]>TIME_NOW || $row["block_time"]==-1?true:false);
					
					$row["block_time"] = ($row["block_time"]>0?date("d/m/Y",$row["block_time"]):-1);					
					
					$row["active"] = Url::build_all(array("cmd","user_id"),"cmd=active&user_id=".$row["id"]);
					
					$row["un_active"] = Url::build_all(array("cmd","user_id"),"cmd=un_active&user_id=".$row["id"]);
					$row["del_cache"]	= Url::build_all(array("cmd","user_id"),"cmd=del_cache&user_id=".$row["id"]);
					
					$items[$row["id"]]=$row;
				}				
			}
		}
		
		if($arrUser_id)
		{
			$str_ids = implode(",",$arrUser_id);
		}
		
		if($str_ids)
		{
			$sql = "SELECT user_id, user_email, note, time, admin_name FROM user_lock WHERE user_id IN($str_ids) ORDER BY id DESC";
			$result = DB::query($sql);
			if($result)
			{
				while ($row=mysql_fetch_assoc($result))
				{
					$lock_log[$row["user_id"]] = $row;
				}
			}	
		}
		
		if($items)
		{
			foreach ($items as $key=>$item)
			{
				if(isset($lock_log[$key]))
				{
					$items[$key]["admin_lock"] = $lock_log[$key]["admin_name"];
					$items[$key]["time_lock"] = date("d/m/y H:i",$lock_log[$key]["time"]);
					$items[$key]["reason_lock"] = $lock_log[$key]["note"];
				}
			}
		}

		$display->add("items",$items);
		$display->add("paging",$paging);		
		$display->add("is_root",User::is_root());
        $display->add('server_img',IMAGE_PATH);
		$display->add("admin_can_edit",User::have_permit(array('user_add','user_edit','user_view','user_delete')));	
		
		$display->add('msg',$this->showFormErrorMessages(1));
		$display->output("UserAdmin");
		$this->endForm();
	}	
	
	function lock_forever($str_ids)
	{
		if($str_ids)
		{
			DB::update("user",array("block_time"=>"-1"),"id IN($str_ids)");
			$reason_lock = "Khóa vĩnh viên từ Admin";				
			$arr_ids = explode(",", $str_ids);
			
			if(count($arr_ids)>0)
			{		
				for($i=0;$i<count($arr_ids);$i++)
				{
					$user = User::getUser($arr_ids[$i]);
					
					$user_email = $user["email"];					
					DB::insert('user_lock',array('time'=>TIME_NOW,'user_id'=>$arr_ids[$i],'user_email'=>$user_email,'type'=>1,'note'=>$reason_lock,'admin_id'=>User::id(),'admin_name'=>User::user_email()));
					
					User::getUser($arr_ids[$i],1);							
				}
			}			
		}
	}
}
?>
